package com.gcb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcb.common.enums.AuthStatusEnum;
import com.gcb.common.enums.CreateTypeEnum;
import com.gcb.common.enums.ProjectTypeEnum;
import com.gcb.common.exception.ServiceException;
import com.gcb.common.utils.OrderUtils;
import com.gcb.domain.bo.ProjectBIBO;
import com.gcb.domain.dto.ProjectQueryDTO;
import com.gcb.domain.dto.ProjectSaveDTO;
import com.gcb.domain.entity.Company;
import com.gcb.domain.entity.Project;
import com.gcb.domain.vo.ProjectVO;
import com.gcb.mapper.CompanyMapper;
import com.gcb.mapper.ProjectMapper;
import com.gcb.service.IProjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 项目信息表
 *
 * @author cz
 * @since 2024-03-06
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService  {

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Override
    public List<ProjectVO> getPageList(ProjectQueryDTO dto) {
        List<ProjectVO> pageList = projectMapper.getPageList(dto);
        pageList.forEach(this::handleVO);
        return pageList;
    }

    @Override
    public List<ProjectVO> getListByCompanyId(Long companyId) {
        if (Objects.isNull(companyId)) {
            return CollUtil.newArrayList();
        }
        ProjectQueryDTO dto = new ProjectQueryDTO();
        dto.setCompanyId(companyId.toString());
        dto.setAuthStatus(3);
        List<ProjectVO> pageList = projectMapper.getPageList(dto);
        pageList.forEach(this::handleVO);
        return pageList;
    }

    private void handleVO(ProjectVO vo){
        if (Objects.nonNull(vo)) {
            vo.setProjectTypeStr(ProjectTypeEnum.getInfo(vo.getProjectType()));
            vo.setAuthStatusStr(AuthStatusEnum.getInfo(vo.getAuthStatus()));
            vo.setCreateTypeStr(CreateTypeEnum.getInfo(vo.getCreateType()));
        }
    }


    @Override
    public void save(ProjectSaveDTO projectDto, Long currentUserId) {
        Company company = companyMapper.selectCompanyById(Long.valueOf(projectDto.getCompanyId()));

        String existName = projectMapper.existNameByCompanyId(projectDto.getProjectName(), company.getId(), null);
        if (StrUtil.isNotBlank(existName)) {
            throw new ServiceException("同一个企业下项目名称不能重复");
        }

        Project project = BeanUtil.copyProperties(projectDto, Project.class);
        String projectNo = OrderUtils.generateOrderNo("PJ");
        project.setProjectNo(projectNo);
        project.setCreateType(1);
        //4.1版本没有审核状态 默认为审核通过
        project.setStatus(AuthStatusEnum.VERIFIED.getCode());
        project.setCompanyId(company.getId());
        project.setCreateBy(currentUserId);
        project.setFullAddress(project.getProvince() + project.getCity() + project.getDistrict() + project.getAddress());
        projectMapper.insert(project);
    }

    @Override
    public void update(ProjectSaveDTO projectDto, Long currentUserId) {
        Company company = companyMapper.selectCompanyById(Long.valueOf(projectDto.getCompanyId()));

        String existName = projectMapper.existNameByCompanyId(projectDto.getProjectName(), company.getId(), projectDto.getId());
        if (StrUtil.isNotBlank(existName)) {
            throw new ServiceException("同一个企业下项目名称不能重复");
        }

        Project project = BeanUtil.copyProperties(projectDto, Project.class);
        project.setCompanyId(company.getId());
        project.setOperatorId(company.getOperateId());
        project.setUpdateBy(currentUserId);
        project.setUpdateTime(new Date());
        project.setFullAddress(project.getProvince() + project.getCity() + project.getDistrict() + project.getAddress());
        projectMapper.updateById(project);
    }

    @Override
    public ProjectVO getDetail(String id) {
        ProjectVO detailVO = projectMapper.getDetail(id);
        detailVO.setProjectTypeStr(ProjectTypeEnum.getInfo(detailVO.getProjectType()));
        detailVO.setAuthStatusStr(AuthStatusEnum.getInfo(detailVO.getAuthStatus()));
        return detailVO;
    }

    @Override
    public Project getById(Long id) {
        return projectMapper.selectById(id);
    }

    @Override
    public List<ProjectBIBO> biList(Integer flag, String city) {
        return projectMapper.selectBIList(flag, city);
    }

    @Override
    public List<Project> list() {
        return projectMapper.selectList(null);
    }
}